$(document).ready(function() {

	$('[on-chat-live]').OnChat({
            api:'http://onchat.co/'
        });

    $('#pegar-valor').jSelectedVal({
        elementeFind: '.box-tipo-pessoa',
        callbackBeforeClick: function($value) {
            var $redirect = url_site + 'cadastre-se/fisica';
            if ($value == 2) {
                $redirect = url_site + 'cadastre-se/juridica';
            }
            window.location = $redirect;
        }
    });
    
    //captcha 
    
    
    
    $.initializeTriggerCaptcha = function(){
    	$('[data-trigger-reload-captcha]').on('click.Captcha',function(e){
        	e.preventDefault();
           var	   $_target =  $(this).attr('data-target-captcha')
                   ,$p =   $.ajax({'url':'home/reload_captcha'});
        	 
                  $p.then(function(html){
                	  $($_target).html(html);
                  });
        });
    };
    
    
   // $.initializeTriggerCaptcha();
    
    
    $('[data-trigger="submit"]').on('click.SubmitFormTrigger',function(e){e.preventDefault();
          var $target = $(this).attr('data-target');
        
          $('form'+$target).submit();
    });


    var htmlNews =
            '<div class="form-group row">'
            + '<div class="col-sm-12"><h3>Cadastre-se e receba os próximos leilões, notícias e novidades. </h3></div>'
            + '<label class="col-sm-2">Nome</label>'
            + '<div class="col-sm-10"><input name="nome" class="form-control" placeholder="Digite o nome" /></div>'
            + '</div>'
            + '<div class="form-group row">'
            + '<label class="col-sm-2">Email</label>'
            + '<div class="col-sm-10"><input name="email" class="form-control" placeholder="Digite o email" /><em>Todos os campos são necessários</em></div>'
            + '</div>'
            + '<div class="form-group row box-captcha">'
            + '<div class="col-sm-offset-2 col-sm-10">'
            + '<div class="row ">'
            + '<div class="col-sm-4 d-image-box-captcha"></div>'
            + '<div class="col-sm-4">'
            + '<input name="captcha" class="form-control" placeholder="" />'
            + '<a data-trigger-reload-captcha data-target-captcha=".d-image-box-captcha" href="#" class="reset-captcha">Clique para ver outra imagem</a>'
            + '</div>'
            + '<div class="col-xs-2">'
            + '<a data-trigger-add-news-letter href="#"><img src="/public/images/ico_enviar.gif" />'
            + '<span class="text-enviar-novidades">Enviar</span></a>'
            + '</div>'
            + '</div>'
            + '<div class="row"><div class="col-sm-12"><i>Digite o código de verificação diferenciando maíusculas e minúsculas</i></div></div>'
            + '</div>'
            + '<div class="clearfix"></div>'
            + '<div class="message"></div>'
            + '</div>'
            ;
    $('[data-trigger="newsletter"]').modalBox({
        data: htmlNews,
        title: 'Receba Novidades',
        id: 'newsLetter',
        callbackBeforeRender: function(e, obj) {
          
            $.ajax({'url': 'home/reload_captcha',success:function(html){
            	
            	   $(obj).find('.d-image-box-captcha').html(html);
            }});       
        },
        callbackAfterRender: function($obj) {
        	 $.initializeTriggerCaptcha();
        	 
        	 $('[data-trigger-add-news-letter]').off('click').on('click',function(){
        		 var $data  = {};
        		 $obj.find('input').each(function(k,v){
        			 var obj  = $(this);
        			 
        			  $data[obj.attr('name')] = obj.val(); 
        			 
        		 }); 
        		 
        		 $.ajax({
        			 'url': url_site +'register-news-letter.html', 
        			 'data':$data,
        			 'type':'post',
        			 'dataType':'json', 
        			 success:function(json){
        				 if(json.success === false){
        					 $($obj).find('.message').append('<div class="messasge-news alert alert-danger">'+json.message+'</div>');
        					 $('.messasge-news').delay(4000).slideUp(function(){$(this).remove();});
        				 }
        				 
        				 if(json.success === true){
        					 $($obj).find('.message').append('<div class="messasge-news alert alert-success">'+json.message+'</div>')
        					 .delay(3000).fadeOut(function(){
        						 $.fn.onCloseModalBox();
        					 });
        					 
        				 }
        				 
        			 }});
        		 
        	 });
        }

    });


    var htmlCadastro = '<div class="form-group row">'
            + '<div class="col-sm-12"><h3>Selecione o tipo de cadastro que deseja fazer:</h3></div>'
            + '</div>'
            + '<div class="radio text-modal-radio">'
            + '<label>'
            + '<input type="radio" name="tipo-pessoa" id="optionsRadios1" value="1" checked>'
            + '<span class="text-pessoa">Cadastro de Pessoa Fisica</sapn>'
            + '</label>'
            + '</div>'
            + '<div class="radio text-modal-radio">'
            + '<label>'
            + '<input type="radio" name="tipo-pessoa" id="optionsRadios2" value="2">'
            + '<span class="text-pessoa">Cadastro de Pessoa Juridica</span>'
            + '</label>'
            + '</div>'
            + '<div class="col-sm-12 row btn-avancar-modal">'
            + '<a href="#" data-trigger="redirecCadastro" class="text-avancar-modal">Avançar<i><img src="/public/images/btn-avancar-modalbox.gif" /></a>'
            + '</div>'
            + '<div class="row img-logo-right">'
            + '<img src="/public/images/logo_modalbox.gif" />'
            + '</div>'
            ;

    $('[data-trigger="cadastro"]').modalBox({
        data: htmlCadastro,
        title: 'Cadastro',
        id: 'cadastro',
        callbackBeforeRender:function(e){
          
        },
        callbackAfterRender: function($obj) {
            var $obj = $obj.attr('class');
            $obj = $obj.split(' ');
            $('[data-trigger="redirecCadastro"]').jSelectedVal({
                elementeFind: '.' + $obj[2],
                callbackBeforeClick: function($value) {
                    var $redirect = url_site + 'cadastre-se/fisica';
                    if ($value == 2) {
                        $redirect = url_site + 'cadastre-se/juridica';
                    }
                    window.location = $redirect;
                }
            });
        }
    });
    
	
	
	var tpl = '<span class="text-fecha-dias">FECHA EM:</span><span class="text-periodo-dias">%%D%% DIA(S)</span>'
            + '<span class="time-img"><img src="/public/images/icon-time-lance.png" /></span>'
            + '&nbsp;<span class="time-lance">%%H%%:%%M%%:%%S%%</span>';
    window.countTime = 0;
    $('.time-count').onTickerTime({
        triggerdate: 'data-count',
        DisplayFormat: tpl,
        update: function() {


        },
        finish: function() {
            alert('Encerrado');
        }
    });

    var tpl = '<span class="time-img"><img src="/public/images/icon-time-lance.png" /></span>'
            + '&nbsp;<span class="time-lance">%%H%%:%%M%%:%%S%%</span>';
    $('.time-count-maxlive').onTickerTime({
        triggerdate: 'data-count',
        DisplayFormat: tpl,
        update: function() {


        },
        finish: function() {
            alert('Encerrado');
        }
    });

    //
    $('[data-trigger="show-box"]').on('click.showBox', function() {

        var elem = $(this).attr('data-target'), obj = $('#' + elem + ', .' + elem),
                wd = obj.width(),
                $attr = obj.attr('style'),
                pat = /\-/gi;
        wd = '-' + wd;
		$(this).find('.ishow,.slabel').removeClass('hide');$(this).css({'left':'-34px'});
		
		$('.iclose').removeClass('hide');
        if (typeof $attr === 'undefined' || pat.test($attr)) {
		    // exibindo dados
			$(this).find('.ishow,.slabel').addClass('hide');
			
            wd = 0;
        }else{
		$('.iclose').addClass('hide');
		$(this).css({'left':'-70px'});
		}

        obj.animate({
            right: wd
        });

    });
	
	
	
$(window).bind('scroll', function() {
       var navHeight = 60;
             if ($(window).scrollTop() > navHeight) {
                 $('.box-painel-2').addClass('painel-fixed');
             }
             else {
                 $('.box-painel-2').removeClass('painel-fixed');
             }
        });
});

(function($) {
    $.fn.jSelectedVal = function(options) {

        var $o = $.extend({}, {
            elementFind: 'body',
            callbackBeforeClick: function($value) {

            }
        }, options || {}),
                $ele = this;

        $($ele).click(function(e) {
            var valor = '';
            e.preventDefault();
            //Executa Loop entre todas as Radio buttons com o name de valor
            $($o.elementFind).find('input[type="radio"]').each(function() {
                //Verifica qual está selecionado
                if ($(this).is(':checked')) {
                    valor = $(this).val();
                }
            });
            $o.callbackBeforeClick(valor);

        });


    };

})(jQuery);


